package com.project.dao.Impl;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.bean.UserBean;
import com.project.dao.ILinkDao;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class LinkDaoImpl extends BaseDao implements ILinkDao {
    @Override
    public CutPageBean<LinkBean> cutAll(int pageNo) {
        CutPageBean<LinkBean> cutBean = new CutPageBean<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_link limit ?,?");
            ps.setObject(1, (pageNo - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2, CutPageBean.PAGESIZE);
            rs = ps.executeQuery();
            while (rs.next()) {
                LinkBean lInkBean = this.getLInk(rs);
                cutBean.getList().add(lInkBean);
            }

            ps = con.prepareStatement("select count(*) c from t_link");
            rs = ps.executeQuery();
            while (rs.next()) {
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
        return cutBean;
    }

    @Override
    public void add(LinkBean linkBean) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_link(I_name,I_birthday,I_phone)" +
                    "values(?,?,?)");

            ps.setObject(1, linkBean.getName());
            ps.setObject(2, linkBean.getBirthday());
            ps.setObject(3, linkBean.getPhone());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }

    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement(" DELETE FROM t_link WHERE pk_linkId= ?");
            ps.setObject(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void update(int id, String phone) {
        this.setConnection();
        try {
            ps=con.prepareStatement("UPDATE t_link SET I_phone = ? WHERE pk_linkId = ?");
            ps.setObject(1,phone);
            ps.setObject(2,id);
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public LinkBean findById(int id) {

            LinkBean linkBean = null;
            this.setConnection();
            try {
                ps=con.prepareStatement("SELECT * FROM t_link where pk_linkId= ?");
                ps.setObject(1,id);
                rs= ps.executeQuery();
                if (rs.next()){
                    linkBean=this.getLInk(rs);
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                this.closeConnection();
            }
            return linkBean;

    }

    private LinkBean getLInk(ResultSet rs) throws SQLException {
        LinkBean lInkBean = new LinkBean();
        lInkBean.setId(rs.getInt("pk_linkId"));
        lInkBean.setName(rs.getString("I_name"));
        lInkBean.setBirthday(LocalDate.parse(rs.getString("I_birthday")));
        lInkBean.setPhone(rs.getString("I_phone"));
        return lInkBean;
    }
}
